import 'dart:math';
import 'dart:ui';

import '../utils/color_utils.dart';
import 'base/base_effect.dart';
import 'effect_template.dart';

class Effect03 extends EditableAnimationEffect {
  @override
  List<Color> get defaultColors {
    return const [
      Color(0xFFFF0000),
      Color(0xFFFFFF00),
      Color(0xFF00FF00),
      Color(0xFF00FFFF),
      Color(0xFF0000FF),
      Color(0xFFFF00FF)
    ];
  }

  @override
  bool get enableAngle => false;

  @override
  bool get enableColorWidth => false;

  @override
  bool get enableDensity => false;

  @override
  bool get enableEditColor => true;

  @override
  bool get enableSpeed => false;

  @override
  RangeArgument get speedRange => RangeArgument(0, 9, 7);

  @override
  bool get isFixedColorCount => false;

  @override
  RangeArgument get colorCountRange => RangeArgument(1, 6, 6);

  @override
  EffectType get effectTyp => EffectType.effect03;

  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) {
    double sizeWidth = size.width;
    double sizeHeight = size.height;
    List<Color> colorList = getColorList(effectOperation: operation);
    //上下一来回算一帧
    //单帧数量: 彩色部分： size.height + 5； 上面变化的点： 10
    int frameHeight = sizeHeight.toInt() + 15;
    //总帧数
    int totalFrameCount = frameHeight * 2;
    int frameCount = (sizeWidth).toInt();
    List<double> stops = [];
    double stop = 0;
    for (int i = 0; i < colorList.length; i++) {
      if (stop > 1) {
        stop = 1;
      }
      stops.add(stop * totalFrameCount - 1);
      stop = stop + 1 / (colorList.length - 1);
    }

    List<List<List<Color>>> frames = [];
    for (int i = 0; i < totalFrameCount; i++) {
      Color gradientColor =
      ColorUtils.lerpGradient(colorList, stops, i.toDouble());
      // Color gradientColor = ColorUtils.lerpGradient(colorList, stops, i.toDouble());

      //单帧的下标位置
      int currentPosition = i <= frameHeight - 1
          ? frameHeight - 1 - i
          : frameHeight - 1 - (totalFrameCount - 1 - i);

      List<List<Color>> frame = [];
      for (int j = 0; j < frameCount; j++) {
        List<Color> colors = [];
        for (int w = 0; w < sizeHeight.toInt(); w++) {
          Color color = getColorFromPos(currentPosition, w.toDouble(),gradientColor);
          colors.add(color);
        }
        frame.add(colors);
      }
      frames.add(frame);
    }
    return Future.value(frames);
  }

  Color getColorFromPos(
      int currentPosition,
      double y,
      Color gradientColor,
      ) {
    //生成上面部分彩色部分的随机点
    int colorRange = Random().nextInt(5);
    //生成上面部分白色部分的随机点
    int randomRange = Random().nextInt(5);

    if (currentPosition < 5) {
      return gradientColor;
    } else if (y >= currentPosition - colorRange - randomRange) {
      if (y >= currentPosition - colorRange) {
        return gradientColor;
      } else {
        return ColorUtils.colorWithBrightness(Color(0xFFFFFFFF), Random().nextDouble());
      }
    } else {
      return Color(0xFF000000);
    }
  }

  @override
  Size get size => const Size(24, 24);
}